package com.lkm.app_ui.ui.sale.p;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.google.gson.annotations.SerializedName;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppUIHelp;
import com.lkm.app_ui.help.TaskHelp;
import com.lkm.app_ui.m.fun.OnKeyNumberEvent;
import com.lkm.app_ui.netapi.AppApi;
import com.lkm.app_ui.view.choiceCard.ChoiceCard;
import com.lkm.app_ui.view.choiceCard.ChoiceItem;
import com.lkm.app_ui.view.choiceCard.IChoiceItem;
import com.lkm.comlib.help.ViewHelp;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.comlib.task2.DefaultTaskExecutor;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.LinkBusEventFlag;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.util.CycleHelp;
import com.lkm.market2_0.entity.BreedTo;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyarddata.OnSteelardInfoLoadEvent;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardserver.event.OnSteelardOnlineEvent;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;



public class GoodsSelectFragment extends MBaseFragment implements ChoiceCard.ChoiceCardBC {


    @Override
    protected boolean configIsLockKey() {
        return true;
    }

    public static GoodsSelectFragment getInstance() {
        GoodsSelectFragment f = new GoodsSelectFragment();
        Bundle data = new Bundle();

        f.setArguments(data);

        return f;
    }


    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_goods_select;
    }

    private ChoiceCard mChoiceCard;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mChoiceCard = findViewById(R.id.ChoiceCard);

        mChoiceCard.init(false, R.layout.view_choice_card2, R.layout.item_fun_mini_p, R.layout.item_fun_mini, 2);
//        mChoiceCard.getGridView().setHorizontalSpacing(ViewHelp.dp2px(getContext(),2));
//        mChoiceCard.getGridView().setVerticalSpacing(ViewHelp.dp2px(getContext(),2));


        mChoiceCard.setChoiceCardBC(this);


        if (RunTimeData.steelyardInfo != null)
            initClass(RunTimeData.steelyardInfo.breedTo);

        reOnSteelardInfoLoadEvent();
    }


    private void reOnSteelardInfoLoadEvent() {

        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelardInfoLoadEvent>() {
            @Override
            public Class<OnSteelardInfoLoadEvent> getBusEventClass() {
                return OnSteelardInfoLoadEvent.class;
            }

            @Override
            public void onReceiver(OnSteelardInfoLoadEvent data) {
                if (data.getSteelyardInfo() != null)
                    initClass(data.getSteelyardInfo().breedTo);
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



    }



    @Override
    public void onChoiceChangeListener(ChoiceCard cc, IChoiceItem c, int index) {
        if (c.getIsChoice()) {
            BreedTo goodsClassTo = (BreedTo) c.getTag();

            SteelyardDataModule steelyardDataModule = AppConst.getModuleManager().peekModule(SteelyardDataModule.class);
            if(steelyardDataModule!=null)
                steelyardDataModule.replaceCurName(goodsClassTo.goods_name);

        }
    }


    private void initClass(List<BreedTo> breedTo) {
        if (breedTo == null) return;
        ChoiceItem[] cis = new ChoiceItem[breedTo.size()];
        int i = 0;
        for (BreedTo funItem : breedTo) {
            cis[i] = new ChoiceItem("" + funItem.id, funItem.goods_name);
            cis[i].setTag(funItem);
            i++;
        }

        mChoiceCard.binData(cis);
    }






    public void uiTips(final String s) {
        if (isExit()) return;
        final Activity activity = getActivity();
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    AppUIHelp.tryShowTipsSHORT(  s);
                }
            });
        }
    }




}//GoodsButtonFragment end